﻿<?php
	include '../inc/conn.php';
	include '../sendmail.php';

	function get_weeks_num(){
		$strTime = date("Y-m-d");
		$intWeek = ceil(((strtotime($strTime) - strtotime(date("Y")."-01-01 00:00:00")))/(7*86400))+1;
		return $intWeek;
	}

	session_start();
	extract($_POST);
	extract($_GET);
	$pagestr = end(explode("/",$_SERVER['PHP_SELF']));
	unset($_POST,$_GET);

	if (isset($act))
	{
		if ($act == 'add' && isset($_SESSION['logname']))
		{
			if (empty($w1) || empty($w2) || empty($w3) || empty($w4) || empty($w5)){
				echo "<script>alert('有周几没有安排人');window.location='$pagestr';</script>";
				die("有周几没有安排人!");
			}
			$wk1=date("Y_")."W".trim(strtoupper($wk));
			$db = new mysql();
			$sql="INSERT INTO `duty_sch` (`week`,`w1`,`w2`,`w3`,`w4`,`w5`) values('${wk1}', '${w1}', '${w2}', '${w3}', '${w4}', '${w5}')";
			$db->query($sql);
			echo "<script>alert('数据添加成功!');window.opener.location.reload();window.location.href='$pagestr'</script>";
			$db->close();
		}
		
		if ($act == 'change' && isset($_SESSION['logname']))
		{	
			$db = new mysql();
			$table = "duty_sch";
			$condition = "ID=${id}";
			$mod_content="`w1`='${w1}', `w2`='${w2}', `w3`='${w3}', `w4`='${w4}', `w5`='${w5}'";
			$db->update($table, $mod_content, $condition);
			echo "<script>alert('内容修改完成!');window.location.href='$pagestr';</script>";
			$db->close();
		}
		
		if ($act == 'mail' && isset($_SESSION['logname']))
		{
			$db = new mysql();
			$sql="select * from duty_sch where ID=$id";
			$db->query($sql);
			$num = $db->db_num_rows();
			$row = $db->fetch_assoc();
			$content ="<b>周别：</b>${row['week']}<br /><b>周一：</b>${row['w1']}<br /><b>周二：</b>${row['w2']}<br /><b>周三：</b>${row['w3']}<br />
						<b>周四：</b>${row['w4']}<br /><b>周五：</b>${row['w5']}<br /> <br /><b>发送：</b>".$_SESSION['logchnname'];
			//SendMail("【".$row['week']."】值班表", $content, "TE");
			$param = array(
				'aSync'=>'Yes',
				'title'=>"【".$row['week']."】值班表",
				'msg'=>$content,
				'to'=>'TE'
			);
			doAsyncMail($param);
			echo "<script>alert('邮件发送完成!');window.location.href='$pagestr';</script>";
			$db->close();
		}
	}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">-->
<link rel="stylesheet" href="/css/bootstrap.min.css">
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/jquery-migrate.min.js"></script>
<script type="text/javascript" src="/xhEditor/xheditor.min.js"></script>
<script type="text/javascript" src="/xhEditor/xheditor_lang/zh-cn.js"></script>
<script type="text/javascript" src="/js/bootstrap.min.js"></script>
<script>
	$(function(){
		$("#ADDDIV").hide();
		$("#ADDDUTY").click(function(){
			$("#ADDDIV").toggle();
		});
	});
</script>
<title>值班管理系统</title>
</head>
<body class="background">
<div class="container-fluid white-background pagecontent">
<?
	if(isset($_SESSION['logname'])){
		if ($pagename=="changeitem"){
			echo "<label>操作项次: </label><a id='ADDDUTY' href='#'> <span class=\"glyphicon glyphicon-plus\"></span>添加</a> | <a id='ADDDUTY' href='$pagestr'> <span class=\"glyphicon glyphicon-backward\"></span>返回</a>";
		}else{
			echo "<label>操作项次: </label><a id='ADDDUTY' href='#'> <span class=\"glyphicon glyphicon-plus\"></span>添加</a>";
		}
	}
	echo "<div id='ADDDIV'>\n";
	echo "<form action='$pagestr?act=add' method='post'>\n";
	echo "	<div>\n";
	echo "	添加值日表:<br />\n";
	echo "		周别 : <input name='wk' id='wk' value='".get_weeks_num()."'><br>\n";
	echo "		周一: <input name='w1' id='w1'><br>\n";
	echo "		周二: <input name='w2' id='w2'><br>\n";
	echo "		周三: <input name='w3' id='w3'><br>\n";
	echo "		周四: <input name='w4' id='w4'><br>\n";
	echo "		周五: <input name='w5' id='w5'><br>\n";
	echo "	</div>\n";
	echo "	<div>\n<input class='btn btn-success btn-primary' name='submit' value='提交' type='submit'>\n<input class='btn btn-danger' name='reset' value='重写' type='reset'>\n</div>\n";
	echo "</form>\n";
	echo "</div>\n";

	// change item page
	if ($pagename=="changeitem"){
		$db = new mysql();
		$sql="select * from duty_sch where ID=$id";
		$db->query($sql);
		$num = $db->db_num_rows();
		$row = $db->fetch_assoc();
		echo "\n<form action='$pagestr?act=change&id=${row['ID']}' method='post'>\n";
		echo "	<div>\n";
		echo "	修改内容:<br />\n";
		echo "		周别 : <input name='wk' id='wk' value='${row['week']}'><br>\n";
		echo "		周一: <input name='w1' id='w1' value='${row['w1']}'><br>\n";
		echo "		周二: <input name='w2' id='w2' value='${row['w2']}'><br>\n";
		echo "		周三: <input name='w3' id='w3' value='${row['w3']}'><br>\n";
		echo "		周四: <input name='w4' id='w4' value='${row['w4']}'><br>\n";
		echo "		周五: <input name='w5' id='w5' value='${row['w5']}'><br>\n";
		echo "	</div>\n";
		echo "	<div>\n<input class='btn btn-success btn-primary' name='submit' value='提交' type='submit'>\n";
		echo "  <input class='btn btn-danger' name='reset' value='重写' type='reset'>\n</div>\n";
		echo "</form>\n";
		$db->close();
	}else{
		$db = new mysql();
		$db->query("select * from duty_sch order by week desc");
		$num = $db->db_num_rows();
		echo "<table border=0 cellpadding=0 class='table table-striped'>";
		echo "<tr class='success'><th>周别</th><th>周一</th><th>周二</th><th>周三</th><th>周四</th><th>周五</th><th>操作</th></tr>";
		for($i=0; $i<$num; $i++){
			$row = $db->fetch_assoc();
			echo "<tr>";
			echo "<td>${row['week']}</td>";
			echo "<td>${row['w1']}</td>";
			echo "<td>${row['w2']}</td>";
			echo "<td>${row['w3']}</td>";
			echo "<td>${row['w4']}</td>";
			echo "<td>${row['w5']}</td>";
			if(isset($_SESSION['logname'])){
				echo "<td><a href='$pagestr?pagename=changeitem&id=${row['ID']}'><span class=\"glyphicon glyphicon-edit\"></span></a> | ";
				echo "<a href='$pagestr?act=mail&id=${row['ID']}'><span class=\"glyphicon glyphicon-envelope\"></span></a></td>";
			}else{
				echo "<td>#</td>";
			}
			echo "</tr>\n";
		}
		echo "</table>";
		$db->close();
	}
?>
</div>
</body>
</html>
